﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RebateRef = zkdao.Host.RebateService;
using TagRef = zkdao.Host.TagService;
using zkdao.Enum;
using sharp_net;
using sharp_net.Mvc;
using PagedList;

namespace zkdao.Host.Controllers {
    public class RebateController : Controller {

        public ActionResult Index(int pageindex = 1) {
            using (var client = new RebateRef.RebateServiceClient()) {
                var count = 0;
                var rebaters = client.GetListPager(ref count, 20, pageindex);
                var rebater_pager = new StaticPagedList<RebateRef.RebateData>(rebaters ?? new RebateRef.RebateData[0], pageindex, 20, count);
                return View(rebater_pager);
            }
        }

        public ActionResult Details(int id) {
            return View();
        }

        [Authorize]
        public ActionResult Submit() {
            InjectDropList();
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Submit(RebateRef.RebateSubmit model) {
            if (ModelState.IsValid) {
                using (var client = new RebateRef.RebateServiceClient()) {
                    client.Submit(
                        new RebateRef.RebateData {
                            Title = model.Title,
                            ContentRich = Server.HtmlEncode(model.ContentRich),
                            HandPrice = model.HandPrice,
                            StartTime = model.StartTime,
                            EndTime = model.EndTime
                        },
                        false,
                        model.ImageIDs.Split(','),
                        model.DiscountPriceTags.AppendSplit(",", model.DiscountSourceTags).AppendSplit(",", model.AreaLimitTags).AppendSplit(",", model.GoodsCategoryTags).Split(',')
                    );
                }
                return RedirectToAction("Index");
            }
            InjectDropList();
            return View(model);
        }

        protected void InjectDropList(){
            using (var client = new TagRef.TagServiceClient()) {
                IEnumerable<TagRef.TagData> tags = client.GetList();
                IEnumerable<EnumItem> GoodsCategoryEnums = tags.Where(t => t.GroupEnum == (int)eTagGroup.GoodsCategory).Select(e => new EnumItem { Text = e.Name, Value = e.ID });
                ViewBag.GoodsCategoryList = GoodsCategoryEnums.ToSelectListItem(s => s.Text, s => s.Value, null, null);
                IEnumerable<EnumItem> DiscountPriceEnums = tags.Where(t => t.GroupEnum == (int)eTagGroup.DiscountPrice).Select(e => new EnumItem { Text = e.Name, Value = e.ID });
                ViewBag.DiscountPriceList = DiscountPriceEnums.ToSelectListItem(s => s.Text, s => s.Value, null, null);
                IEnumerable<EnumItem> DiscountSourceEnums = tags.Where(t => t.GroupEnum == (int)eTagGroup.DiscountSource).Select(e => new EnumItem { Text = e.Name, Value = e.ID });
                ViewBag.DiscountSourceList = DiscountSourceEnums.ToSelectListItem(s => s.Text, s => s.Value, null, null);
                IEnumerable<EnumItem> AreaLimitEnums = tags.Where(t => t.GroupEnum == (int)eTagGroup.AreaLimit).Select(e => new EnumItem { Text = e.Name, Value = e.ID });
                ViewBag.AreaLimitList = AreaLimitEnums.ToSelectListItem(s => s.Text, s => s.Value, null, null);
            }
        }

        [Authorize]
        public ActionResult Edit(int id) {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection) {
            try {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            } catch {
                return View();
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection) {
            try {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            } catch {
                return View();
            }
        }
    }
}
